<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>书籍列表</title>
    <script type="text/javascript" src="lib/js/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="lib/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="lib/css/bootstrap.min.css">
    <link rel="stylesheet" href="lib/layui/css/layui.css">
    <script type="text/javascript" src="lib/layui/layui.js"></script>
    <link rel="stylesheet" href="app/css/tablegroup.css">
    <script type="text/javascript" src="lib/js/js.cookie.min.js"></script>

</head>
<body>
<div class="container">
    <div class="tgroup">
        <div class="form form-inline">
            <input class="form-control" type="text" name="" id="bookname" placeholder="名称">
            <input class="form-control" type="text" name="" id="author" placeholder="作者">
            <input class="form-control" type="text" name="" id="publisher" placeholder="出版社">
            <button class="btn btn-primary">查询</button>
        </div>
    </div>


    <table class="layui-table" id="bktb" lay-filter="bktb">

    </table>
</div>
<div id="borrowp" >
    <form class="layui-form" action="">

        <div class="layui-form-item">

            <label class="layui-form-label" style="width: 200px;padding-left: 0;">借阅<span style="color:#6cf;word-wrap:break-word" id="bname"></span></label>
            <div class="layui-input-inline">
                <select name="quiz1" id="daynum">
                    <option value="">请选择借阅天数</option>
                    <option value="7" selected="">7</option>
                    <option value="30">30</option>
                    <option value="100">100</option>
                </select>
            </div>
            <div class="layui-form-mid layui-word-aux">天</div>
        </div>





    </form>
</div>

</div>
</body>
</html>
<script >
    $(document).ready(function () {
        layui.use('table', function () {
            var table = layui.table;
            table.render({
                elem: '#bktb',
                url: '/libms/book/list',
                method: 'post',
                page: true,

                cols: [[
                    {field: 'name', title: '名称'},
                    {field: 'author', title: '作者'},
                    {field: 'publisher', title: '出版社'},
                    {field: 'edition', title: '版次'},
                    {field: 'num', title: '数量',},
                    {fixed: 'right', width: 200, align: 'center', toolbar: '#barDemo', title: '操作'}
                ]]


            });
            //监听工具条
            table.on('tool(bktb)', function (obj) {
                var data = obj.data;
                if (obj.event === 'borrow') {
                    // layer.msg('ID：' + data.id + ' 的借阅操作');
                    var cu = Cookies.get("user");
                    console.log(cu);


                    if (cu) {
                        var user = JSON.parse(cu);
                        $("#bname").val(data.name);
                        layer.open({
                            type: 1,
                            content: $('#borrowp'),
                            btn: ['确定', '取消'],
                            area: ['500px', '300px'],
                            btn1: function () {
                                $.ajax({
                                    url: '/libms/borrow/add',
                                    type: 'post',
                                    dataType: 'json',
                                    data: {
                                        bid: data.id,
                                        uid:cu.id,
                                        days:$("#daynum").val(),
                                    },
                                })
                                .done(function() {
                                    console.log("success");
                                })
                                .fail(function() {
                                    console.log("error");
                                })
                                .always(function() {
                                    console.log("complete");
                                });




                                // var ac = false;
                                // var d = $(".bookinput");
                                // var data = rDate(d);
                                // console.log(data);
                                // $.ajax({
                                //     url: url,
                                //     type: 'post',
                                //     dataType: 'json',
                                //     data: data,
                                //     async: false
                                // }).done(function (e) {
                                //     if (e.code == 1) {
                                //         ac = true;
                                //         table.reload("bktb")
                                //     } else {
                                //         alert(e.msg)
                                //     }
                                //     console.log("success");
                                // })
                                //     .fail(function () {
                                //         alert("操作失败")
                                //     })
                                //     .always(function () {
                                //         console.log("complete");
                                //     });
                                // return true;
                            },
                            end: function () {
                                $(".bookinput").val("");
                            }

                        });

                        
                    } else {
                        alert("请登录")
                    }
                }
                // else if (obj.event === 'del') {
                //     layer.confirm('真的删除行么', function (index) {
                //         obj.del();
                //         layer.close(index);
                //     });
                // } else if (obj.event === 'edit') {
                //     layer.alert('编辑行：<br>' + JSON.stringify(data))
                // }
            });
        });




    });
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="borrow">借阅</a>
    <!--<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>-->
    <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->

</script>